package org.ls.petclinic.web;

import cn.hutool.Hutool;
import cn.hutool.core.date.DateUtil;
import org.ls.petclinic.dao.AccessDao;
import org.ls.petclinic.dao.CustomerDao;
import org.ls.petclinic.dao.impl.AccessDaoImpl;
import org.ls.petclinic.dao.impl.CustomerDaoImpl;
import org.ls.petclinic.pojo.Access;
import org.ls.petclinic.untils.JdbcUtils;
import org.ls.petclinic.untils.WebUtils;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * @Description
 * @Author 浪矢先生
 * @Create 2022-06-10 22:51
 */
@WebServlet(urlPatterns = "/addAccess")
public class AddAccess extends BaseServlet{
    private CustomerDao customerDao=new CustomerDaoImpl();
    private AccessDao accessDao=new AccessDaoImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        //宠物主人id
        int id = WebUtils.parseInt(req.getParameter("id"), 0);
        //访客信息
        String name = req.getParameter("name");
        String phone = req.getParameter("phone");
        //添加访客表记录
        int add = accessDao.add(new Access(null, id, name, phone, DateUtil.now()));
        JdbcUtils.commitAndClose();
        if (add==1){
            req.setAttribute("msg", "记录访客成功!");
        }else {
            req.setAttribute("msg", "记录访客出错!");
        }
        //页面转发
        req.getRequestDispatcher("/jsp/getPet.jsp").forward(req, resp);
//        resp.sendRedirect(req.getContextPath() + "/jsp/getPet.jsp");
    }
}
